"""
    HTTP 服务端
"""
#练习
from socket import *

def handle(connfd):
    # 接收HTTP请求
    request=connfd.recv(1024).decode()
    if not request:
        return #浏览器断开则结束
    info = request.split(" ")[1]
    #组织响应
    try:
        with open("."+info,'rb') as file:
            data = file.read()#图片内容
    except:
        return

    response="HTTP/1.1 200 OK\r\n"
    response+= "Content-Type:image/jpeg\r\n"
    response+="\r\n"
    response=response.encode()+data
    connfd.send(response)


def main():
    sock=socket()
    sock.bind(("0.0.0.0",60000))
    sock.listen(5)

    while True:
        #等待浏览器连接
        connfd,addr=sock.accept()
        print("Connect from",addr)

        handle(connfd)#处理浏览器请求
        connfd.close()

if __name__ == '__main__':
    main()
































































